/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mahumLogs.beans;

import com.mahumLogs.backedbeans.LogsCalendarDate;
import com.mahumLogs.backedbeans.MonthLog;
import com.mahumLogs.backedbeans.TopSite;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.enterprise.context.Dependent;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.UserTransaction;

/**
 *
 * @author Kalast
 */
@Named
@Dependent
public class LogsCalendarBean {
    
    @PersistenceContext
    private EntityManager em;
    
    @Resource
    private UserTransaction tx;
    
    public static final MonthLog[] MONTHS = new MonthLog[]{
            new MonthLog(0, "Janvier", "m_janlog"),
            new MonthLog(1, "Février", "m_feblog"),
            new MonthLog(2, "Mars", "m_marlog"),
            new MonthLog(3, "Avril", "m_aprlog"),
            new MonthLog(4, "Mai", "m_maylog"),
            new MonthLog(5, "Juin", "m_junlog"),
            new MonthLog(6, "Juillet", "m_jullog"),
            new MonthLog(7, "Août", "m_auglog"),
            new MonthLog(8, "Septembre", "m_seplog"),
            new MonthLog(9, "Octobre", "m_octlog"),
            new MonthLog(10, "Novembre", "m_novlog"),
            new MonthLog(11, "Décembre", "m_declog")
        };
    
    
    public static String getMonthCSSOf(int month){
        return MONTHS[month-1].getClasseCSS();
    }
    
    public static String getMonthNameOf(int month){
        return MONTHS[month-1].getName();
    }
    
    public List<LogsCalendarDate> getLogsCalendarMonths(){
        System.out.println("Start requete calendar month");
        List<TopSite> topSites = em.createQuery("SELECT ts FROM TopSite ts GROUP BY ts.mois,ts.annee ORDER BY ts.annee,ts.mois ASC").getResultList();
        ArrayList<LogsCalendarDate> lcds = new ArrayList();
        System.out.println("Onr recup " + topSites.size() +" ts de mois");
        for(TopSite ts : topSites){
            lcds.add(new LogsCalendarDate(ts));
        }
        return lcds;
    }
    
     public List<LogsCalendarDate> getLogsCalendarDates(LogsCalendarDate lcd){
        System.out.println("Start requete calendar");
        long time = System.currentTimeMillis();
        
        /*List<Long> resultList = em.createQuery("SELECT l.atTime.time FROM Log l GROUP BY FUNC('FLOOR',l.atTime.time / 86400000)")
            .getResultList();*/
        Query query = em.createQuery("SELECT ts FROM TopSite ts WHERE"
                + " ts.annee=" + lcd.yearLog
                + " AND ts.mois=" + lcd.monthLog
                + " GROUP BY ts.mois,ts.annee,ts.jour ORDER BY ts.annee,ts.mois,ts.jour ASC");
        List<TopSite> topSites = query.getResultList();
        ArrayList<LogsCalendarDate> lcds = new ArrayList();
        
        for(TopSite ts : topSites){
            lcds.add(new LogsCalendarDate(ts));
        }
        /*for(Long l : resultList){
            System.out.println(l);
            //calendar.addDay(l.getMonthLog()-1, l.getDayLog());
        }*/
        System.out.println("Temps de la requete : " + (((double)System.currentTimeMillis() - (double)time)/1000f) + " secs");
        return lcds;
    }
}
